package de.farbstreich.tozen2.client;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ClientGUILoginPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton connectionButton = null;
	private JLabel serverAddressLabel = null;
	private JTextField serverAddressTextField = null;
	private JLabel usernameLabel = null;
	private JTextField usernameTextField = null;
	public ClientGUIInput guiInput;

	public ClientGUILoginPanel(ClientGUIInput guiInput) {
		super();
		this.guiInput = guiInput;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraintsUsernameTextField = new GridBagConstraints();
		gridBagConstraintsUsernameTextField.fill = GridBagConstraints.BOTH;
		gridBagConstraintsUsernameTextField.gridy = 1;
		gridBagConstraintsUsernameTextField.weightx = 1.0;
		gridBagConstraintsUsernameTextField.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsUsernameTextField.gridx = 1;
		GridBagConstraints gridBagConstraintsUsernameLabel = new GridBagConstraints();
		gridBagConstraintsUsernameLabel.gridx = 0;
		gridBagConstraintsUsernameLabel.gridy = 1;
		usernameLabel = new JLabel();
		usernameLabel.setText("Benutzername:");
		GridBagConstraints gridBagConstraintsServerAddressTextField = new GridBagConstraints();
		gridBagConstraintsServerAddressTextField.fill = GridBagConstraints.BOTH;
		gridBagConstraintsServerAddressTextField.gridy = 0;
		gridBagConstraintsServerAddressTextField.weightx = 1.0;
		gridBagConstraintsServerAddressTextField.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsServerAddressTextField.gridx = 1;
		GridBagConstraints gridBagConstraintsServerAddressLabel = new GridBagConstraints();
		gridBagConstraintsServerAddressLabel.gridx = 0;
		gridBagConstraintsServerAddressLabel.anchor = GridBagConstraints.EAST;
		gridBagConstraintsServerAddressLabel.gridy = 0;
		serverAddressLabel = new JLabel();
		serverAddressLabel.setText("Server:");
		GridBagConstraints gridBagConstraintsConnectionButton = new GridBagConstraints();
		gridBagConstraintsConnectionButton.gridx = 1;
		gridBagConstraintsConnectionButton.gridy = 4;
		this.setSize(255, 200);
		this.setLayout(new GridBagLayout());
		this.add(getConnectionButton(), gridBagConstraintsConnectionButton);
		this.add(serverAddressLabel, gridBagConstraintsServerAddressLabel);
		this.add(getServerAddressTextField(),
				gridBagConstraintsServerAddressTextField);
		this.add(usernameLabel, gridBagConstraintsUsernameLabel);
		this.add(getUsernameTextField(), gridBagConstraintsUsernameTextField);
	}

	/**
	 * This method initializes Button to connect
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getConnectionButton() {
		if (connectionButton == null) {
			connectionButton = new JButton();
			connectionButton.setText("Anmelden");
			Formator.formateButton(connectionButton, 100);
			connectionButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.out.println(usernameTextField.getText()
									+ "@" + serverAddressTextField.getText());
							assert guiInput == null : "Error: ClientGUILoginPanel.getConnectionButton => no guiInput";
							if (serverAddressTextField.getText().isEmpty()) {
								ErrorMessage
										.showErrorMessage(
												serverAddressTextField,
												"Bitte gebe eine korrekte Serveradresse ein.");
							}
							if (usernameTextField.getText().isEmpty()) {
								ErrorMessage.showErrorMessage(
										usernameTextField,
										"Bitte gebe einen Namen ein.");
							}
							if (serverAddressTextField.getText().isEmpty()
									|| usernameTextField.getText().isEmpty()) {
								return;
							}
							guiInput.setConnection(serverAddressTextField
									.getText(), usernameTextField.getText());
						}
					});
		}
		return connectionButton;
	}

	/**
	 * This method initializes Server Adsress TextField
	 * 
	 * @return javax.swing.JTextField
	 */
	public JTextField getServerAddressTextField() {
		if (serverAddressTextField == null) {
			serverAddressTextField = new JTextField();
			serverAddressTextField.setText("127.0.0.1");
			Formator.formateComponent(serverAddressTextField);
		}
		return serverAddressTextField;
	}

	/**
	 * This method initializes Username TextField1
	 * 
	 * @return javax.swing.JTextField
	 */
	public JTextField getUsernameTextField() {
		if (usernameTextField == null) {
			usernameTextField = new JTextField();
			Formator.formateComponent(usernameTextField);
		}
		return usernameTextField;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
